cd "/Users/omerf.orsun/Google Drive/Research/War Effort/Replication Files Dataverse/"
clear
use Data.dta

*********************************************
*Table 1 and Figure 2************************
*********************************************
eststo clear
*Table 1, Base Model
xi: eststo: xtreg M LM  C I W i.year,fe    cl(ccode)
keep if e(sample)
nlcom (exp(_b[I])+.0027428-1)*100
nlcom (exp(_b[C])+.0027428-1)*100
keep if e(sample)

*Table 1, Null Model
xi: eststo: xtreg M LM  C I WC WI W i.year,fe    cl(ccode)

**Table 1, Null Model, Row 1: Null Marginal Effect of Contestation| War - Logarithmic Scale 
lincom C+WC
**Table 1, Null Model, Row 2: Marginal Effect of Inclusiveness| War- Logarithmic Scale
lincom I+WI

*Table 1, Plus Wealth Model
xi: eststo: xtreg M LM  C I WC WI W GDPPC i.year,fe    cl(ccode)
*Table 1, Plus Wealth Model, Row 1: Null Marginal Effect of Contestation| War - Logarithmic Scale 
lincom C+WC
*Table 1, Plus Wealth Model, Row 2: Marginal Effect of Inclusiveness| War- Logarithmic Scale
lincom I+WI

*Table 1, Plus Credit Rating Model
xi: eststo: xtreg M LM  C I WC WI W IIR i.year,fe    cl(ccode)
*Table 1, Plus Credit Rating  Model, Row 1: Null Marginal Effect of Contestation| War - Logarithmic Scale 
lincom C+WC
*Table 1, Plus Credit Rating  Model, Row 2: Marginal Effect of Inclusiveness| War- Logarithmic Scale
lincom I+WI

*Table 1, Plus Cap. of Allies Model
xi: eststo: xtreg M LM  C I WC WI W allycap i.year,fe    cl(ccode)
*Table 1, Plus Cap. of Allies Model, Row 1: Null Marginal Effect of Contestation| War - Logarithmic Scale 
lincom C+WC
*Table 1, Plus Cap. of Allies Model, Row 2: Marginal Effect of Inclusiveness| War- Logarithmic Scale
lincom I+WI

*Table 1, Plus Cap. of Rivals Model
xi: eststo: xtreg M LM  C I WC WI W srivalcap i.year,fe    cl(ccode)
*Table 1, Plus Cap. of Rivals Model, Row 1: Null Marginal Effect of Contestation| War - Logarithmic Scale 
lincom C+WC
*Table 1, Plus Cap. of Rivals Model, Row 2: Marginal Effect of Inclusiveness| War- Logarithmic Scale
lincom I+WI

*Table 1, Plus Military Regimes Model
xi: eststo: xtreg M LM  C I WC WI W   gwf_military   i.year,fe    cl(ccode)
*Table 1, Plus Military Regimes Model, Row 1: Null Marginal Effect of Contestation| War - Logarithmic Scale 
lincom C+WC
*Table 1, Plus Military Regimes Model, Row 2: Marginal Effect of Inclusiveness| War- Logarithmic Scale
lincom I+WI

*TABLE 1 Results
esttab, b(a2) se(2) replace label star(* 0.10 ** 0.05 *** 0.01) scalars(N r2_w N_g)   varwidth(25) modelwidth(6)       ///
order(C I WC WI W  GDPPC IIR allycap srivalcap gwf_military LM) ///
drop(*year*) ///
nogap 


*********************************************
*Table 2*************************************
*********************************************
xi: eststo: xtreg M LM  C I WC WI W i.year,fe    cl(ccode)

*Table 2, Contestation - Wartime Defense Expenditure  
nlcom (exp(_b[C]+_b[WC])+.0027428-1)*100 
*Table 2, Contestation - Peacetime Defense Expenditure  
nlcom (exp(_b[C] )+.0027428-1)*100 
*Table 2, Contestation - Difference between War and Peace
nlcom ((exp(_b[C]+_b[WC])+.0027428-1)*100-(exp(_b[C])+.0027428-1)*100)


*Table 2, Inclusiveness - Wartime Defense Expenditure  
nlcom (exp(_b[I]+_b[WI])+.0027428-1)*100
*Table 2, Inclusiveness - Peacetime Defense Expenditure  
nlcom (exp(_b[I] )+.0027428-1)*100
*Table 2, Inclusiveness - Difference between War and Peace
nlcom ((exp(_b[I]+_b[WI])+.0027428-1)*100-(exp(_b[I])+.0027428-1)*100)


*********************************************
*Appendix Table A1***************************
*********************************************
clear
use Data.dta
xtreg M LM  C I W i.year,fe    cl(ccode)
keep if e(sample)
su M C I W IIR GDPPC allycap srivalcap gwf_military  



*********************************************
*Appendix Table A2***************************
*********************************************
clear
use Data.dta
eststo:  xtreg M LM   C I WC WI W    i.year,fe  cl(ccode) 
xtunitroot fisher M if e(sample), dfuller lags(1)


*********************************************
*Appendix Table A3***************************
*********************************************
clear
use WarInvolvement.dta
eststo clear
eststo:  probit war    unconttenurespell uncontdimC  tenurespell lcap t_*
esttab, b(a2) se(2) replace label star(* 0.10 ** 0.05 *** 0.01) scalars(N r2_p)   varwidth(25) modelwidth(6)       ///
nogap 


*********************************************
*Appendix Figure A1**************************
*********************************************
estsimp  probit war    unconttenurespell uncontdimC  tenurespell lcap t_*
setx   t_1 0 t_2 0 t_3 0 
capture program drop clarifyinteractcat
program define clarifyinteractcat
capture drop *MU  *LI *HI xaxis_* yline
qui su `1',d
scalar min`1'=r(`4')
scalar max`1'=r(`5')
qui g `1'MU=.
qui g `1'LI=.
qui g `1'HI=.
qui g xaxis_`2'=.
qui su `2'
qui levelsof `2'
qui gen zxy=r(levels)
qui split zxy, p(" ")
qui local n= r(nvars)
qui destring, replace
forvalues i=1/`n'{
qui replace xaxis_`2'=zxy`i'[`i'] in `i'
qui drop zxy`i'
qui scalar xaxis_`2'_`i'=xaxis_`2'[`i']
	qui setx `1' min`1' `2' xaxis_`2'_`i' `3' min`1'*xaxis_`2'_`i'
	qui set seed 12345
	qui simqi, prval(1) genpr(pimin)
	qui setx `1' max`1' `2' xaxis_`2'_`i' `3' max`1'*xaxis_`2'_`i'
	qui set seed 12345
	qui simqi, prval(1) genpr(pimax)
	qui gen diff=pimax-pimin
	qui _pctile diff, p(2.5,97.5)
	qui replace `1'LI = r(r1) in `i'
	qui replace `1'HI = r(r2) in `i'
	qui su diff
	qui replace `1'MU=r(mean) in `i'
	qui drop diff pimax pimin
	}
drop zxy
qui su `1',d 
scalar min`1'=r(`4') //X at 5th Percentile
scalar max`1'=r(`5') //X at 95th Percentile

qui su `2',d
scalar min`2'=r(`4') //Z at 5th Percentile
scalar max`2'=r(`5') //Z at 5th Percentile

	qui setx `1' max`1' `2' min`2' `3' max`1'*min`2'  //X=p95, Z=p5
	qui set seed 12345 // sets seed
	qui simqi, prval(1) genpr(p1a) // P(Y|X=p95, Z=p5)
	qui setx `1' min`1' `2' min`2' `3' min`1'*min`2'  //X=p5, Z=p5
	qui set seed 12345 // sets seed
	qui simqi, prval(1) genpr(p1b) // P(Y|X=p5, Z=p5)
	qui setx `1' max`1' `2' max`2' `3' max`1'*max`2'  //X=p95, Z=p95
	qui set seed 12345 // sets seed
	qui simqi, prval(1) genpr(p2a) // P(Y|X=p95, Z=p95)
	qui setx `1' min`1' `2' max`2' `3' min`1'*max`2'  //X=p5, Z=p95
	qui set seed 12345 // sets seed
	qui simqi, prval(1) genpr(p2b) // P(Y|X=p5, Z=p95)
	qui g P2minusP1=(p2a-p2b)-(p1a-p1b) // P2-P1
	/*where P1=P(Y|X=p95,Uncertainty=p5)-P(Y|X=p5,Uncertainty=p5)
	        P2=P(Y|X=p95,Uncertainty=p95)-P(Y|X=p5,Uncertainty=p95) */
	qui _pctile P2minusP1, p(5,95)
	scalar P2_P1_LCI=r(r1)
	scalar P2_P1_HCI=r(r2)
	qui su P2minusP1
	scalar P2_P1_Mean=r(mean)
	drop P2minusP1 p1a p1b p2a p2b


g yline=0 if xaxis_`2'!=.
twoway (line yline xaxis_`2', clwidth(thin) clcolor(black) clpattern(dash)) (rcap `1'HI `1'LI  xaxis_`2')  (scatter `1'MU xaxis_`2', msymbol(circle)), xlabel(#`n') ytitle("Pr(Y=1)") note("Difference: `=P2_P1_Mean', (`=P2_P1_LCI',  `=P2_P1_HCI')") scheme(s1mono)	          
capture drop *MU  *LI *HI xaxis_* yline
end

clarifyinteractcat uncontdimC tenurespell unconttenurespell p5 p95
drop `e(allsims)'

*********************************************
*Appendix Table A4***************************
*********************************************
clear
use Data.dta
eststo clear
*Military Expenditure as a Share of Government Spending
xi: eststo: xtreg Mg LMg  C I WC WI W i.year,fe    cl(ccode)
lincom C+WC
lincom I+WI
*Excluding Post-Soviet Countries
xi: eststo: xtreg M LM  C I WC WI W i.year if GEO!="Post Soviet",fe    cl(ccode)
lincom C+WC
lincom I+WI

*W    *************************************************** FİX THİS
xi: eststo: xtreg M LM C w WC Ww W    i.year,fe
lincom C+WC
lincom w+Ww

*Vanhanen Measure
xi: eststo: xtreg M LM CV IV WCV  WIV W    i.year,fe
lincom CV+WCV
lincom IV+WIV

*Contestation Only
xi: eststo: xtreg M LM  C   WC   W  i.year, fe   cl(ccode)
lincom C+WC

*Inclusiveness Only
xi: eststo: xtreg M LM    I   WI W  i.year, fe   cl(ccode)
lincom I+WI

*TABLE A4 Results
esttab, b(a2) se(2) replace label star(* 0.10 ** 0.05 *** 0.01) scalars(N r2_w N_g)   varwidth(25) modelwidth(6) order(C I WC WI W w Ww CV IV WCV WIV)     ///
drop(*year*) ///
nogap 



*********************************************
*OTHER MENTIONED QUANTITITES*****************
*********************************************

*Note for lag-selection: BIC Tests
clear
use Data.dta
eststo:  xtreg M LM C I WC WI W L1.(C I WC WI W) L2.(C I WC WI W) L3.(C I WC WI W) i.year,fe  cl(ccode) 
g sample=1 if e(sample)==1
estat ic
eststo:  xtreg M LM C I WC WI W L1.(C I WC WI W) L2.(C I WC WI W)   i.year if sample==1,fe  cl(ccode) 
estat ic
eststo:  xtreg M LM C I WC WI W L1.(C I WC WI W)    i.year if sample==1,fe  cl(ccode) 
estat ic
eststo:  xtreg M LM C I WC WI W     i.year if sample==1,fe  cl(ccode) 
estat ic

**Note for Classical Standard Errors
clear
use Data.dta
xtreg M LM C I WC WI W    i.year,fe
lincom C+WC
lincom I+WI


**Note for Panel Corrected Standard Errors
clear
use Data.dta
xtpcse M  C I W WC WI, pairwise corr(psar1)
lincom C+WC
lincom I+WI


*Note for System Generalized Method of Moments
clear
use Data.dta
xi: eststo: xtabond2 M LM  C I WC WI W  i.year, gmm(L4.LM ,c) iv(C I WC WI W  i.year )  two  
lincom C+WC
lincom I+WI
esttab, b(a2) se(2) replace label star(* 0.10 ** 0.05 *** 0.01) scalars(N hansenp ar1p ar2p j N_g) r2   varwidth(25) modelwidth(6) ///
order(C I WC WI W ) ///
drop(*year*) ///
nogap 

